package com.jinzhi.jzweb.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.CapitalFlowDO;
import com.jinzhi.jzweb.service.CapitalFlowService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 
 * <pre>
 * 资金流水表
 * </pre>
 * <small> 2020-02-24 21:00:10 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/capitalFlow")
public class
 CapitalFlowController extends BaseController {
	@Autowired
	private CapitalFlowService capitalFlowService;
	@Autowired
	private UserService userService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:capitalFlow:capitalFlow")
	String CapitalFlow(){
	    return "jzweb/capitalFlow/capitalFlow";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:capitalFlow:capitalFlow")
	public Result<Page<CapitalFlowDO>> list(CapitalFlowDO capitalFlowDTO){
		Wrapper<CapitalFlowDO> wrapper = new EntityWrapper<CapitalFlowDO>().orderBy("id", false);
		if (capitalFlowDTO.getState() != null){
			wrapper.eq("state",capitalFlowDTO.getState());
		}
		if (capitalFlowDTO.getUserType() != null){
			wrapper.eq("user_type",capitalFlowDTO.getUserType());
		}
		if (capitalFlowDTO.getSigns() != null){
			wrapper.eq("signs",capitalFlowDTO.getSigns());
		}
		if (capitalFlowDTO.getType() != null){
			wrapper.eq("type",capitalFlowDTO.getType());
		}
		if (capitalFlowDTO.getPayType() != null){
			wrapper.eq("pay_type",capitalFlowDTO.getPayType());
		}
		if (StringUtils.isNotBlank(capitalFlowDTO.getUserName())){
			Wrapper<UserDO> uWrapper = new EntityWrapper<UserDO>().like("name",capitalFlowDTO.getUserName());
			List<UserDO>  userDOS = userService.selectList(uWrapper);
			String str = "";
			for (UserDO userDO : userDOS){
				str = str + userDO.getId() + ",";
			}
			wrapper.and();
			if(StringUtils.isNotBlank(str)){
				wrapper.in("user_id",str.substring(0,str.length()-1));
			}else{
				wrapper.eq("user_id",null);
			}
		}

		Page<CapitalFlowDO> page = capitalFlowService.selectPage(getPage(CapitalFlowDO.class), wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			CapitalFlowDO obj = (CapitalFlowDO) page.getRecords().get(i);
			if (obj.getUserId() != null){
				UserDO userDO = userService.selectById(obj.getUserId());
				if (userDO != null) {
					obj.setUserName(userDO.getName());
				}
			}
			if (obj.getPayUserId() != null){
				UserDO userDO = userService.selectById(obj.getPayUserId());
				if (userDO != null) {
					obj.setPayUserName(userDO.getUsername());
				}
			}
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:capitalFlow:add")
	String add(){
	    return "jzweb/capitalFlow/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:capitalFlow:edit")
	String edit(@PathVariable("id") Long id,Model model){
		CapitalFlowDO capitalFlow = capitalFlowService.selectById(id);
		model.addAttribute("capitalFlow", capitalFlow);
	    return "jzweb/capitalFlow/edit";
	}
	
	@Log("添加资金流水表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:capitalFlow:add")
	public Result<String> save( CapitalFlowDO capitalFlow){
		capitalFlowService.insert(capitalFlow);
        return Result.ok();
	}
	
	@Log("修改资金流水表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:capitalFlow:edit")
	public Result<String>  update( CapitalFlowDO capitalFlow){
		boolean update = capitalFlowService.updateById(capitalFlow);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除资金流水表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:capitalFlow:remove")
	public Result<String>  remove( Long id){
		capitalFlowService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除资金流水表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:capitalFlow:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		capitalFlowService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
